<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../css/jquery-ui.css">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影院详细地址</label>
        <div class="layui-input-block">
            <input type="text" id="cid" hidden>
            <input type="text" id="address" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入影院地址" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影院名</label>
        <div class="layui-input-block">
            <input type="text" id="cname" name="address" lay-verify="required"  value="" class="layui-input" disabled>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影厅</label>
        <div class="layui-input-block">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-primary demo1">
                    <label id="hall_name">请选择影厅</label>
                    <label id="hall_id" hidden>-1</label>
                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">电影</label>
        <div class="layui-input-block">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-primary demo2">
                    <label id="movie_id" hidden>-1</label>
                    <label id="movie_name">请选择电影</label>
                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">放映时间</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="start" placeholder="放映开始时间">
        </div>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="end" placeholder="放映结束时间" disabled>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">添加场次</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-ui.min.js" charset="utf-8"></script>
<script>
    function formatDate(date) {
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear(),
            hour = '' + d.getHours(),
            min = '' + d.getMinutes();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
        if (hour.length < 2) hour = '0' + hour;
        if (min.length < 2) min = '0' + min;
        return [year, month, day].join('-')+" "+hour+":"+min;
    }

    function setEndTime(value){
        let movieId = $("#movie_id").text();
        $.post('${path}/movies/admin/findByMovieId',{"id":movieId},function (data){
            let time = data;
            let rawdate = new Date(value.replace(/-/g, '/'))
            let new_time = new Date(rawdate.setMinutes(rawdate.getMinutes()+parseInt(time)));
            let end = formatDate(new_time);
            $('#end').val(end);
        })
    }
    jQuery(function ($) {
        $(function () {
            $("#address").autocomplete({
                serviceUrl : 'autoComplete',//load from server
                type:'post',
                paramName:'ciname',
                select: function(event,ui) {
                    // event 是当前事件对象
                    let address = ui.item.value
                    $.post('${path}/cinames/findByAddress',{"address":address},function (data) {
                        $("#cname").val(data.ciname.name);
                        $("#cid").val(data.ciname.id);
                        layui.use(['dropdown', 'layer'], function () {
                            let dropdown = layui.dropdown
                                , $ = layui.jquery;

                            dropdown.render({
                                elem: '.demo1'
                                , data: data.halls
                                , click: function (obj) {
                                    $("#hall_name").text(obj.title)
                                    $("#hall_id").text(obj.id)

                                }
                            });

                            dropdown.render({
                                elem: '.demo2'
                                , data: data.movies
                                , click: function (obj) {
                                    $("#movie_name").text(obj.title)
                                    $("#movie_id").text(obj.id)
                                    console.log(obj.id)
                                    let start = $("#start").val()
                                    if(start!=null&&start.trim().length>0){
                                        setEndTime($("#start").val())
                                    }
                                }
                            });
                        })
                    })
                    // 该对象具有label和value属性，以及其它自定义(如果有的话)的属性
                },
                source: function(request,response) {
                    $.ajax({
                        url: '${path}/cinames/findAllAddress',
                        dataType: "json",
                        data: request,
                        success: function(data) {
                            response(data);
                        },
                    });
                },

            });
        });

    });
    // 设置最小可选的日期
    function minDate(){
        let now = new Date();
        return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate();
    }


    layui.use('laydate', function(){
        let laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
            ,min: minDate()
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
            ,done:(value) => {
                let movieId = $("#movie_id").text();
                setEndTime(value)

                //执行指定方法
            }
        });
    });

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            layer.confirm('确认是否添加?',{
                title: '添加场次',
                icon:'3'
            },function (index){
                let send_data = {"cinameId":$("#cid").val(),"hallId":$("#hall_id").text(),"movieId":$("#movie_id").text(),
                                "start":$("#start").val(),"end":$("#end").val()}
                $.post("${path}/session/save",send_data,function (result){
                    if(result.code=="200"){
                        layer.msg('添加成功！', {icon:1, time: 2000},function (){
                            parent.location.reload();
                            // 关闭弹出层
                            layer.close(index);
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    }else {
                        layer.msg('添加失败！', {icon:2});
                    }
                });
            })

            return false;
        });
    });



</script>
</body>
</html>